package ui.activity.mine;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dispatchapp.andlei.R;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import adapter.MyWalletAdapter;
import base.BaseBean;
import base.activity.BaseLayoutActivity;
import bean.MultipleItem;
import bean.RiderInfoBean;
import bean.UserWalletBean;
import netWork.NetUtils;
import utils.GsonUtils;
import utils.SPUtils;

/**
 * 我的钱包
 *
 * @author Andlei
 * @DATE 2019/1/22
 */
public class MyWalletActivity extends BaseLayoutActivity {

    private TextView tv_balance, tv_award_money,tv_history_order;
    private ImageView img_tixian;
    private TextView tv_finish_order, tv_has_dao, tv_finish_award, tv_out_money;
    private NetUtils netUtils;
    private LinearLayout layout_shouzhi_detail, layout_bond;
    private List<MultipleItem> itemList = new ArrayList<>();
    private MyWalletAdapter walletAdapter;
    private RecyclerView recy_list;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setBaseRightText("规则说明");
        setTitle("我的钱包");
        setTitleBg(R.color.color_333333);
        setTitlefontBg();

    }

    @Override
    protected void onResume() {
        super.onResume();
        setTitleBg(R.color.color_333333);
        setTitlefontBg();
        netUtils.get("rider/user/wallet", BaseBean.class, false);
        netUtils.post("rider/user/rider-info", BaseBean.class, false);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_wallet;
    }

    @Override
    protected void findViews() {
        netUtils = new NetUtils(mActivity, netRequestCallBack);
//        tv_today_shouru = getView(R.id.tv_today_shouru);
//        tv_today_zhichu = getView(R.id.tv_today_zhichu);
        tv_balance = getView(R.id.tv_balance);
        tv_award_money = getView(R.id.tv_award_money);
//        tv_shouxufei = getView(R.id.tv_shouxufei);
        img_tixian = getView(R.id.img_tixian);
        layout_shouzhi_detail = getView(R.id.layout_shouzhi_detail);
        layout_bond = getView(R.id.layout_bond);

        tv_history_order = getView(R.id.tv_history_order);
        tv_finish_order = getView(R.id.tv_finish_order);
        tv_has_dao = getView(R.id.tv_has_dao);
        tv_finish_award = getView(R.id.tv_finish_award);
        tv_out_money = getView(R.id.tv_out_money);
        recy_list = getView(R.id.recy_list);
    }

    @Override
    protected void formatViews() {
        img_tixian.setOnClickListener(this);
        tv_history_order.setOnClickListener(this);
        layout_shouzhi_detail.setOnClickListener(this);
        layout_bond.setOnClickListener(this);

//        for (int i = 0;i<10;i++){
//            MultipleItem item;
//            if(i<5){
//                 item = new MultipleItem(1);
//
//            }else {
//                 item = new MultipleItem(2);
//            }
//            itemList.add(item);
//        }
        recy_list.setLayoutManager(new LinearLayoutManager(mActivity));
        walletAdapter = new MyWalletAdapter(itemList);
        recy_list.setAdapter(walletAdapter);
    }

    @Override
    protected void formatData() {
        netUtils.get("rider/user/wallet", BaseBean.class, false);
    }

    @Override
    protected void getBundle(Bundle bundle) {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.base_right_text:
                jumpTo(RuleActivity.class);
                break;
            case R.id.tv_history_order:
                //收支明细
                jumpTo(ReceiptsDetailsActivity.class);
                break;
            case R.id.img_tixian:
                if (TextUtils.isEmpty(SPUtils.getInstance(mActivity).getString("open_id"))) {
                    jumpTo(BindWeCharActivity.class);
                } else {
                    //提现
                    Bundle bundle = new Bundle();
                    bundle.putString("balance", "" + tv_balance.getText());
                    jumpTo(CashWithdrawalActivity.class, bundle);
                }
                break;
            case R.id.layout_shouzhi_detail:
                //收支明细
                jumpTo(ReceiptsDetailsActivity.class);
                break;
            case R.id.layout_bond:
                //保证金
                jumpTo(BondActivity.class);
                break;
        }
    }

    private NetUtils.NetRequestCallBack netRequestCallBack = new NetUtils.NetRequestCallBack() {
        @Override
        public void error(String action, Throwable e, Map tag) {

        }

        @Override
        public void success(String action, BaseBean t, Map tag) {
            if (action.equals("rider/user/rider-info")) {
                if (t.getCode() == 200) {
                    RiderInfoBean bean = (RiderInfoBean) GsonUtils.getInstance(mContext).parseComplexJArrayByCommon(t, RiderInfoBean.class);
                    SPUtils.getInstance(mActivity).put("open_id", bean.getOpen_id());
                }
            } else {
                UserWalletBean walletBean = (UserWalletBean) GsonUtils.getInstance(mActivity).parseComplexJArrayByCommon(t, UserWalletBean.class);
                try {
                    tv_balance.setText(changeF2Y(walletBean.getBalance()) + "   ");
//                    tv_today_shouru.setText(changeF2Y(walletBean.getToday()));
//                    tv_today_zhichu.setText("-"+changeF2Y(walletBean.getSpend()));
                    tv_award_money.setText("还有" + changeF2Y(walletBean.getReward()) + "元奖励次月10号到账");
//                    tv_shouxufei.setText("提现后1个工作日到账，手续费"+changeF2Y(walletBean.getPoundage())+"元/笔");
                } catch (Exception e) {
                    e.printStackTrace();
                    toast("金额格式有误");
                }

            }

        }
    };
    /**
     * @Title: changeF2Y
     * @Description: 将分为单位的转换为元 （除100）
     * @param amount
     * @throws Exception
     * @return: String
     */
    /**
     * 金额为分的格式
     */
    public static final String CURRENCY_FEN_REGEX = "\\-?[0-9]+";

    public static String changeF2Y(String amount) throws Exception {
        if(TextUtils.isEmpty(amount)){
            return "0";
        }
        if (!amount.matches(CURRENCY_FEN_REGEX)) {
            throw new Exception("金额格式有误");
        }
        return BigDecimal.valueOf(Long.valueOf(amount)).divide(new BigDecimal(100)).toString();
    }
}
